# -*- coding:UTF-8 -*-

"""
main.py
包含一些ECC解密基础操作、文件读写操作
"""
import seccure


testkey_pr = "i am test key"
testkey_pub = "CRwhC^:1<1g.<fYk#FcG:^E}M"

class user_ecc():
    def __init__(self,filename):      #初始化用户ca
        ca = {}
        self.name = filename
        try:
            self.ca_file = open(filename)
        except:
            print("fail to open key file")
        if self.ca_file != None:
            info_e = self.ca_file.read()
            try:
                info_t = seccure.decrypt(info_e,testkey_pr)
            except:
                print("decrypt fail")
            if info_t != None:
                info = info_t.decode("base64").split(";")
                ca['name'] = info[0]
                ca['pub'] = info[2].decode("base64")
                ca['pr'] = info[1].decode("base64")
                self.ca = ca
                print ca

    def ecc_decrypt(self,data):      #私钥解密
        try:
            s = seccure.decrypt(data,self.ca['pr'])
            return s
        except:
            print("praivt key not correct")

    def ecc_encrypt(self,data):      #公钥加密
        s = seccure.encrypt(data,self.ca['pub'])
        return s

    def get_user(self):
        return self.name

if __name__ == '__main__':
    user = user_ecc("bob.key")
    print user.ca



